<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>js-trailing-closure-toy-compiler</title>
    <style>
        .panel {
            margin: 0 auto;
            width: 960px;
        }
        .main {
            display: flex;
        }
        .left, .right {
            flex: 1 50%;
            font-size: 16px;
        }
        .btn-wrapper {
            margin: 0 36px;
            width: 100px;
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: center;
        }
        .container {
            width: 360px;
            height: 500px;
            padding: 18px;
        }
        .raw-code-container {
            background-color: #efefef;
        }
        .result-container {
            background-color: #ececec;
        }
        .btn {
            width: 96px;
            height: 36px;
        }
        .transformer-btn {
            font-weight: bold;
        }
        .empty-btn {
            margin-top: 36px;
        }
        .err-container {
            padding: 12px;
            color: red;
            font-weight: bold;
            background-color: #efefef;
        }
    </style>
    <script src="./js-trailing-closure-toy-compiler.js"></script>
</head>
<body>
    <div class="panel">
        <h1>JavaScript Trailing Closure Toy Compiler</h1>
        <p class="err-container" id="err-container"></p>
        <div class="main">
            <div class="left">
                <div class="raw-code-container container" contenteditable id="raw-code"></div>
            </div>
            <div class="btn-wrapper">
                <button class="transformer-btn btn" id="transformer-btn">编译</button>
                <button class="empty-btn btn" id="empty-btn">清空</button>
            </div>
            <div class="right">
                <div id="result" class="result-container container"></div>
            </div>
        </div>
        <p>
            <a href="https://github.com/dreamapplehappy/js-trailing-closure-toy-compiler">JavaScript Trailing Closure Toy Compiler</a>
            is created by
            <a href="https://github.com/dreamapplehappy">dreamapplehappy</a>
            （注：仅供学习交流使用）
        </p>
    </div>
    <script>
        const el = (id) => {
            return document.getElementById(id);
        }
        const rawCodeContainer = el('raw-code');
        const result = el('result')
        const btn = el('transformer-btn')
        const removeBtn = el('empty-btn')
        const errContainer = el('err-container')
        errContainer.style.display = 'none';

        // 初始化
        rawCodeContainer.innerText = `a()
a{}
a(1, "hello")
a(1, "hello"){}
a(1, "hello"){ e, f in
}

a(1, "hello"){ b, c in
  d()
  d{}
  d(1, "hello")
  d(1, "hello"){}
  d(1, "hello"){ e, f in
    g()
  }
}`

        removeBtn.addEventListener('click', () => {
            rawCodeContainer.innerText = '';
            result.innerText = '';
        })

        btn.addEventListener('click', () => {
            console.log(rawCodeContainer.innerText)
            const rawCode = rawCodeContainer.innerText
            if (rawCode) {
                try {
                    const resultCode = compiler(rawCode);
                    const codeContainer = document.createElement('code');
                    codeContainer.innerText = resultCode;
                    result.innerText = '';
                    result.appendChild(codeContainer);
                    errContainer.style.display = 'none';
                } catch (e) {
                    errContainer.style.display = 'block';
                    errContainer.innerText = `编译出错：${e.message}`;
                }
            }
        })
    </script>
</body>
</html>
